/**
 * 课程详情路由 - Expo Router
 * 课程学习页面，包含各种练习类型
 * 使用 Expo Router 的动态路由和参数传递
 */
import React, { useState, useEffect, useCallback } from 'react';
import {
  View,
  Text,
  StyleSheet,
  TouchableOpacity,
  Alert,
  BackHandler,
} from 'react-native';
import { useRouter, useLocalSearchParams } from 'expo-router';
import AsyncStorage from '@react-native-async-storage/async-storage';

// 导入课程屏幕组件
import LessonScreen from '../screens/LessonScreen';

/**
 * 课程路由组件 - 包装课程屏幕以适配 Expo Router
 */
export default function LessonRoute() {
  const router = useRouter();
  const params = useLocalSearchParams();

  // 处理返回按键
  useEffect(() => {
    const backAction = () => {
      Alert.alert('确认退出', '确定要退出当前课程吗？进度将不会保存。', [
        { text: '取消', onPress: () => null, style: 'cancel' },
        { text: '确定', onPress: () => router.back() },
      ]);
      return true;
    };

    const backHandler = BackHandler.addEventListener('hardwareBackPress', backAction);
    return () => backHandler.remove();
  }, [router]);

  return (
    <LessonScreen 
      route={{ 
        params: {
          lessonId: params.lessonId,
          title: params.title
        }
      }}
      navigation={{ 
        goBack: () => router.back(),
        navigate: (screen, params) => {
          if (screen === 'Home') {
            router.push('/');
          }
        }
      }}
    />
  );
}